package com.dms.modules.user.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 用户缓存工具类
 */
@Component
public class UserCacheUtil {
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    // 缓存键前缀
    private static final String USER_PREFIX = "user:";
    
    // 缓存过期时间（小时）
    private static final long CACHE_EXPIRE_HOURS = 24;
    
    /**
     * 设置缓存
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value, CACHE_EXPIRE_HOURS, TimeUnit.HOURS);
    }
    
    /**
     * 获取缓存
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    
    /**
     * 删除缓存
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }
    
    /**
     * 设置过期时间
     */
    public void expire(String key, long timeout, TimeUnit unit) {
        redisTemplate.expire(key, timeout, unit);
    }
    
    /**
     * 获取用户行为缓存键
     */
    public String getUserBehaviorKey(Long userId) {
        return USER_PREFIX + "behavior:" + userId;
    }
    
    /**
     * 获取用户浏览历史缓存键
     */
    public String getUserBrowseKey(Long userId) {
        return USER_PREFIX + "browse:" + userId;
    }
    
    /**
     * 获取用户收藏缓存键
     */
    public String getUserFavoriteKey(Long userId) {
        return USER_PREFIX + "favorite:" + userId;
    }
    
    /**
     * 获取用户搜索历史缓存键
     */
    public String getUserSearchKey(Long userId) {
        return USER_PREFIX + "search:" + userId;
    }
} 